package com.liwq.base.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author : liwq
 * @version : 1.0
 * @date : 2023/2/21
 **/
public class CyclicBarrierTest2 {
    public static void main(String[] args) {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("七龙珠集齐，神龙出现");
        });

        for (int i = 1; i <= 7; i++) {
            final int a = i;
            new Thread(() -> {
                System.out.println("收集龙珠" + a);
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            },String.valueOf(a)).start();

        }
    }
}
